{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 时间复杂度－例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def find_max(arr):\n",
    "    \"\"\"寻找最大值\"\"\"\n",
    "    max_value = arr[0]          # 1步\n",
    "    sum_steps = 0               # 1步\n",
    "    for i in range(1, len(arr)):\n",
    "        sum_steps += 1          # n-1步\n",
    "        if max_value < arr[i]:   \n",
    "            max_value = arr[i]\n",
    "    print(\"一共%d步\" % sum_steps)\n",
    "    return max_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(find_max([3,4,9,10,1,8,90,45,11]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常见算法时间复杂度比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(1, 11):\n",
    "    print(\"log{0:^2}:{1:.2f} | {0:^2}:{2:^2} | {0:^2}long{0:^2}:{3:^6.2f} | {0:^2}^2:{4:^3} | {0}!:{5}\".format(i,math.log(i, 2),i,i*math.log(i, 2), i*i, math.factorial(i)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 空间复杂度例子－字符串相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "url1 = 'www.' + 'python' + '.com'\n",
    "url2 = ''.join(['www.','python', '.com'])\n",
    "url3 = '{0}{1}{2}'.format('www.','python', '.com')\n",
    "print(url1,url2,url3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  单元测试例子－获取整数的所有约数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_divisors1(n):\n",
    "    \"\"\"\n",
    "    错误例子\n",
    "    输入是一个正整数n\n",
    "    返回n的所有约数，包括1和n\n",
    "    \"\"\"\n",
    "    if n < 1:\n",
    "        return [] # n必须是>=1\n",
    "\n",
    "    answer = []  # 保存结果\n",
    "    for divisor in range(1, n + 1):\n",
    "        if n % divisor == 0:\n",
    "            answer.append(divisor)\n",
    "    return answer\n",
    "\n",
    "def get_divisors2(n):\n",
    "    \"\"\"\n",
    "    正确例子\n",
    "    输入是一个正整数n\n",
    "    返回n的所有约数，包括1和n\n",
    "    \"\"\"\n",
    "    if type(n) != int:\n",
    "        return [] # n必须是整数\n",
    "    if n < 1:\n",
    "        return [] # n必须是>=1\n",
    "    answer = []  # 保存结果\n",
    "    for divisor in range(1, n + 1):\n",
    "        if n % divisor == 0:\n",
    "            answer.append(divisor)\n",
    "    return answer \n",
    "\n",
    "# 单元测试\n",
    "import unittest\n",
    "class TestDivisors(unittest.TestCase):\n",
    "    def test_one(self):\n",
    "        '''\n",
    "        测试输入4\n",
    "        期待结果：[1,2,4]\n",
    "        '''\n",
    "        result = get_divisors1(4)\n",
    "        self.assertEqual(len(result), 3)\n",
    "        self.assertEqual(result[0], 1)\n",
    "        self.assertEqual(result[1], 2)\n",
    "        self.assertEqual(result[2], 4)\n",
    "\n",
    "    def test_two(self):\n",
    "        '''\n",
    "        测试输入-1\n",
    "        期待结果：[]\n",
    "        '''\n",
    "        result = get_divisors1(-1)\n",
    "        self.assertEqual(len(result), 0)\n",
    "\n",
    "    def test_three(self):\n",
    "        '''\n",
    "        测试输入abc\n",
    "        期待结果：[]\n",
    "        '''\n",
    "        result = get_divisors1(\"abc\")\n",
    "        self.assertEqual(len(result), 0)\n",
    "\n",
    "    def test_four(self):\n",
    "        '''\n",
    "        测试输入一个质数113\n",
    "        期待结果：[1,113]\n",
    "        '''\n",
    "        result = get_divisors1(113)\n",
    "        self.assertEqual(result[-1],113)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    # __main__可以理解为这个程序入口\n",
    "    unittest.main()  # 运行所有测试"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  挑战：这个月有多少天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def month_days(year,month):\n",
    "    if year>0 and 1<=month<=12:\n",
    "        if month in [1,3,5,7,8,10,12]:\n",
    "            return 31\n",
    "        if month in [4,6,9,11]:\n",
    "            return 30\n",
    "        if year % 100 == 0:\n",
    "            if year % 400 == 0:\n",
    "                return 29\n",
    "            else:\n",
    "                return 28\n",
    "        else:\n",
    "            if year % 4 == 0:\n",
    "                return 29\n",
    "            else:\n",
    "                return 28\n",
    "    return \"输入有误\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import calendar\n",
    "print(calendar.monthrange(2000,2)[1], month_days(2000,2))\n",
    "print(calendar.monthrange(1999,2)[1], month_days(1999,2))\n",
    "print(calendar.monthrange(2019,7)[1], month_days(2019,7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a=4+3\n",
    "print(\"4+3\", a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dir(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"hello\".capitalize()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
